package services;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import domain.RaumBelegung;
import domain.Universitaet;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.Version;


/**
 * Interface fuer das Schreiben in das Dateisystem als HTML-Datei
 * 
 * @author Patrick Nielinger 
 * @version 14.12.14
 */
public class HTMLOutputService implements OutputService
{
	private static final Logger logger = LoggerFactory.getLogger(HTMLOutputService.class);
	
	
    private Universitaet universitaet ;
    private Configuration cfg;
	private String dozentenPlaeneOutputName;
	private String raumPlaeneOutputName;
	private String studiengangPlaeneOutputName;
	private BusinessServicesProvider bsp;


    /**
     * Instanzmethode welche das zentrale Datenhaltungsobjekt Universitaet und den Konfigurationservice uebergeben bekommt.
     * @throws IOException 
     */
    public HTMLOutputService(Universitaet universitaet,ConfigurationService configurationService, BusinessServicesProvider bsp) throws IOException
    {
        this.universitaet = universitaet;
        this.bsp = bsp;
        
        cfg = new Configuration();
        
        // Where do we load the templates from: --> one directory up
        cfg.setClassForTemplateLoading(HTMLOutputService.class, "..");
        
        // Some other recommended settings:
       // cfg.setIncompatibleImprovements(new Version(2, 3, 20));
        cfg.setDefaultEncoding("UTF-8");
        cfg.setLocale(Locale.GERMAN);
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        

		Properties config = configurationService.getPropValues();
		dozentenPlaeneOutputName = config.getProperty("dozentenPlaeneOutputName").toString();
		raumPlaeneOutputName = config.getProperty("raumPlaeneOutputName").toString();
		studiengangPlaeneOutputName = config.getProperty("studiengangPlaeneOutputName").toString();
    }

	public void schreibeRaumPlaene() throws Exception {
	    //  Prepare the template input:
	    Map<String, Object> input = new HashMap<String, Object>();

	    input.put("uni", this.universitaet);
	    
	    for(RaumBelegung rb : this.universitaet.getRaumbelegungen()){
    		String tag = new SimpleDateFormat("EEEE", Locale.GERMAN).format(rb.getVon());
			String von = new SimpleDateFormat("HH:mm", Locale.GERMAN).format(rb.getVon());
	        String bis = new SimpleDateFormat("HH:mm", Locale.GERMAN).format(rb.getBis());
		  
			  rb.tagString = tag;
			  rb.vonString = von;
			  rb.bisString = bis;
	    }
	    
	    // 2.2. Get the template

	    Template template = cfg.getTemplate("templates/RaumPlaeneTemplate.ftl");
	      
	    File newFile = new File("C:\\srv\\" + raumPlaeneOutputName +  ".html");
	    newFile.getParentFile().mkdirs();
	    
	    Writer fileWriter ;
	    fileWriter = new FileWriter(newFile);
		try {
			template.process(input, fileWriter);
			
		} catch (Exception e) {
			logger.error("Der Plan konnte nicht geschrieben werden ", e);
		}
	   finally {
		   fileWriter.close();
	    }
		
	}

	public void schreibeStudiengangPlaene() throws Exception {

	}
	
	public void schreibeDozentenPlaene() throws Exception{

	}

  
}
